package com.mz.ozy.utils;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;


import com.mz.ozy.bean.DbBean;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/4.
 * 数据库管理类
 */
public class DBManager2 {

    private String DB_NAME = "areatable.db";
    private Context mContext;

    public DBManager2(Context mContext) {
        this.mContext = mContext;
    }

    //把assets目录下的db文件复制到dbpath下
    public SQLiteDatabase DBManager2() {
//        if (Environment.getExternalStorageDirectory().getAbsolutePath())
        String dbPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ozyapp/" + DB_NAME;
        if (!new File(dbPath).exists()) {
            try {
                boolean flag = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ozyapp/").mkdirs();
                boolean newFile = new File(dbPath).createNewFile();
                try {
                    FileOutputStream out = new FileOutputStream(dbPath);
                    InputStream in = mContext.getAssets().open(DB_NAME);
                    byte[] buffer = new byte[1024];
                    int readBytes = 0;
                    while ((readBytes = in.read(buffer)) != -1)
                        out.write(buffer, 0, readBytes);
                    in.close();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return SQLiteDatabase.openOrCreateDatabase(dbPath, null);
    }

    //查询
    public List<DbBean> query(SQLiteDatabase db, String str) {
        List<DbBean> models = new ArrayList<>();
        try {
            String table = "areatable";
            Cursor cursor = db.query(table, new String[]{"AreaId", "AreaName", "AreaLevel", "ParentId"}, "AreaLevel=?", new String[]{str}, null, null, null);
            while (cursor.moveToNext()) {
                String areaName = cursor.getString(cursor.getColumnIndex("AreaName"));
                String areaLevel = cursor.getString(cursor.getColumnIndex("AreaLevel"));
                String areaId = cursor.getString(cursor.getColumnIndex("AreaId"));
                String parentId = cursor.getString(cursor.getColumnIndex("ParentId"));
                DbBean model = new DbBean(areaId, areaName, areaLevel, parentId);
                models.add(model);
            }
            cursor.close();
            return models;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String query2(SQLiteDatabase db, String areaId) {
        try {
            String areaName = "";
            String table = "areatable";
            Cursor cursor = db.query(table, new String[]{"AreaId", "AreaName", "AreaLevel", "ParentId"}, "AreaId=?", new String[]{areaId}, null, null, null);
            while (cursor.moveToNext()) {
                areaName = cursor.getString(cursor.getColumnIndex("AreaName"));
            }
            cursor.close();
            return areaName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


}
